package com.aidl.demo.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.aidl.demo.R;
import com.aidl.demo.entity.Video;
import com.aidl.demo.ui.adapter.MyGridViewAdapter;
import com.aidl.demo.ui.adapter.MyViewPagerAdapter;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/6/23 0023.
 */

public class MovieListActivivty extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.movie_list_viewpager)
    ViewPager movieListViewpager;
    @BindView(R.id.movie_list_edit)
    EditText movieListEdit;
    @BindView(R.id.btn_go)
    Button btnGo;
    @BindView(R.id.movie_list_pager)
    TextView movieListPager;
    @BindView(R.id.movie_list_tatol)
    TextView movieListTatol;

    /**
     * GridView + Viewpager
     */
    private List<GridView> gridViews = new ArrayList<>();
    private List<Video> stringList = new ArrayList<>();
    private int index = 0;
    private int num;
    private int position = 0;
    private MyViewPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        if (getIntent().getSerializableExtra("videolist") == null) {
//            Toast.makeText(this, getString(R.string.network_error), Toast.LENGTH_SHORT).show();
            return;
        } else {
            stringList = (List<Video>) getIntent().getSerializableExtra("videolist");
            initView();
            initData();
        }
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_movie_list;
    }

    @Override
    protected void initView() {
        movieListViewpager.setOnPageChangeListener(new MyOnPagerChanger());
        btnGo.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        //模拟数据
        int size = stringList.size();

        //根据数据判断页数
        if (size % 10 == 0) {
            num = size / 10;
        } else {
            num = size / 10 + 1;
        }

        movieListTatol.setText("/共" + num + "页");

        int temp = 0;
        //页数指定加载固定数据
        for (int i = 0; i < num; i++) {
            temp = position;
            if (position + 10 > stringList.size()) {
                position = stringList.size();
            } else {
                position = position + 10;
            }
            List<Video> subList = this.stringList.subList(temp, position);

            //加载GridView根据页数加载数量
            GridView gridView = new GridView(this);
            gridView.setAdapter(new MyGridViewAdapter(this, subList));
            gridView.setGravity(Gravity.CENTER);
            gridView.setClickable(true);
            gridView.setFocusable(true);
            gridView.setNumColumns(5);      //每一排显示数量
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Video video = stringList.get((index * 10) + position);
                    Intent intent = new Intent(MovieListActivivty.this, MovieInfoActivity.class);
                    intent.putExtra("video", video);
                    startActivity(intent);
//                    Toast.makeText(MovieListActivivty.this, "点击了第" + position + "个位置", Toast.LENGTH_SHORT).show();
                }
            });
            gridViews.add(gridView);        //添加到GridView集合中
            Log.d("MovieListActivivty", "gridViews.size():" + gridViews.size());
        }
        if (gridViews == null || gridViews.size() == 0) {
            return;
        }
        //GridView集合添加到viewpager中
        adapter = new MyViewPagerAdapter(gridViews, this);
        movieListViewpager.setAdapter(adapter);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_go:
                if (movieListEdit.getText().toString() == null || movieListEdit.getText().toString().equals("")){
                    return;
                }else {
                    int GoPager = Integer.parseInt(movieListEdit.getText().toString()) - 1;
                    if (GoPager > num || GoPager < 0 || movieListEdit.getText().toString().equals("")) {
                        return;
                    } else {
                        try {
                            //viewpager在其他页面跳转指定页面时候，防止先跳到初始页面然后在跳到指定页面
                            Field filed = movieListViewpager.getClass().getField("mCurItem");//参数mCurItem是系统自带的
                            filed.setAccessible(true);
                            filed.setInt(movieListViewpager, GoPager);
                        } catch (NoSuchFieldException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                        adapter.notifyDataSetChanged();
                        movieListViewpager.setCurrentItem(GoPager);
                    }
                }
                break;
        }
    }

    class MyOnPagerChanger implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            //viewpager页面改变监听
            index = position;
            movieListPager.setText("当前第" + (index + 1) + "页");
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

}
